package com.ug_project.adapters

import android.view.View
import android.view.ViewGroup
import androidx.viewpager.widget.PagerAdapter
import com.ug_project.objects.isNotNull
import java.lang.Exception

/**
 * Created by Administrator on 3/10/2017.
 */
open class PagerAdapter(
        val views: Array<View>,
        val title_s: Array<String>? = null,
        val pageWidth: Float = 1.0f) : PagerAdapter() {
    init {
        title_s.isNotNull {
            if (it.size < views.size) throw Exception("Title نىڭ مەزمۇنى چوقۇم برىلگەن View نىڭ سانىغا تەڭ بولىشى كىرەك")
        }
    }

    override fun isViewFromObject(view: View, `object`: Any): Boolean {
        return view == `object`
    }

    override fun getCount(): Int {
        return views.size
    }

    override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
        container.removeView(views[position])
    }

    override fun instantiateItem(container: ViewGroup, position: Int): Any {
        container.addView(views[position])
        return views[position]
    }

    override fun getItemPosition(`object`: Any): Int {
        return super.getItemPosition(`object`)
    }

    override fun getPageTitle(position: Int): CharSequence {
        return title_s?.get(position) ?: ""
    }

    override fun getPageWidth(position: Int): Float {
        return pageWidth
    }
}

fun createViewPagerAdapter(views: Array<View>, title_s: Array<String>? = null): com.ug_project.adapters.PagerAdapter {
    var temp = com.ug_project.adapters.PagerAdapter(views, title_s)
    return temp;
}

fun createViewPagerAdapter(views: Array<View>, title_s: Array<String>? = null, pageWidth: Float = 1f): com.ug_project.adapters.PagerAdapter {
    var temp = com.ug_project.adapters.PagerAdapter(views, title_s,pageWidth)
    return temp;
}